﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exceptions
{
    class StudentException : ApplicationException
    {
        public StudentException(string message, Exception innerException)
            : base(null, innerException)
        {
        }

        public StudentException(int? age, string name, int? grade, Exception innerException)
            : base(null, innerException)
        {
            this.age = age;
            this.name = name;
            this.grade = grade;
        }

        public override string Message
        {
            get
            {
                string result = "Invalid student:";

                if (age != null)
                {
                    result += string.Format("\nThe age {0} is not valid", age.Value);
                }

                if (name != null)
                {
                    result += string.Format("\nThe name \"{0}\" has not valid length - {1}", name, name.Length);
                }

                if (grade != null)
                {
                    result += string.Format("\nThe grade {0} is not valid", grade.Value);
                }

                return result;
            }
        }

        private int? age;
        private string name;
        private int? grade;
    }

    enum Gender
    {
        Male,
        Female
    }

    class Student
    {
        const int MIN_AGE = 7;
        const int MAX_AGE = 18;
        const int MIN_NAME_LEN = 10;
        const int MAX_NAME_LEN = 50;
        const int MIN_GRADE = 1;
        const int MAX_GRADE = 11;

        public Student()
            : this(MIN_AGE, default(Gender), new string('x', MIN_NAME_LEN), MIN_GRADE)
        {
        }

        public Student(int age, Gender gender, string name, int grade)
        {
            Age = age;
            Gender = gender;
            Name = name;
            Grade = grade;
        }

        public int Age
        {
            get
            {
                return age;
            }
            set
            {
                if (value < MIN_AGE || value > MAX_AGE)
                {
                    throw new StudentException(value, null, null, null);
                }

                age = value;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }
            set
            {
                if (value.Length < MIN_NAME_LEN || value.Length > MAX_NAME_LEN)
                {
                    throw new StudentException(null, value, null, null);
                }

                name = value;
            }
        }

        public int Grade
        {
            get
            {
                return grade;
            }
            set
            {
                if (value < MIN_GRADE || value > MAX_GRADE)
                {
                    throw new StudentException(null, null, value, null);
                }

                grade = value;
            }
        }

        public Gender Gender
        {
            get
            {
                return gender;
            }
            set
            {
                gender = value;
            }
        }

        private int age;
        private Gender gender;
        private string name;
        private int grade;
    }

    class Exceptions
    {
        static void Main(string[] args)
        {
            Student student = new Student();
        }
    }
}
